// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcBezierCylinder.h"
#include "MgcTriMesh.h"

MgcImplementRTTI(MgcBezierCylinder,MgcBezierPatch);
MgcImplementStream(MgcBezierCylinder);

//----------------------------------------------------------------------------
MgcBezierCylinder::MgcBezierCylinder (unsigned int uiDegree,
    unsigned int uiIndexQuantity, unsigned int* auiIndex)
    :
    MgcBezierPatch(uiDegree,uiIndexQuantity,auiIndex)
{
    m_uiCylinderLevel = 0;
}
//----------------------------------------------------------------------------
MgcBezierCylinder::MgcBezierCylinder ()
{
}
//----------------------------------------------------------------------------
void MgcBezierCylinder::GenerateConnectivity (unsigned int uiLevel,
    MgcTriMesh* pkMesh, unsigned int& ruiTriangleStart)
{
    // generate connectivity array and put in proper location in trimesh

    unsigned int uiTwoPowL = (1 << uiLevel);
    unsigned int uiTwoPowLPlusOne = uiTwoPowL + 1;
    unsigned int uiTwoPowC = (1 << m_uiCylinderLevel);
    unsigned int uiTwoPowCPlusOne = uiTwoPowC + 1;
    unsigned int* puiConnect = pkMesh->Connectivity() + 3*ruiTriangleStart;
    for (unsigned int uiY = 0, uiYStart = 0; uiY < uiTwoPowC; uiY++)
    {
        unsigned int uiI0 = uiYStart;
        uiYStart += uiTwoPowLPlusOne;
        unsigned int uiI1 = uiI0 + 1;
        unsigned int uiI2 = uiYStart;
        unsigned int uiI3 = uiI2 + 1;
        for (unsigned int uiX = 0; uiX < uiTwoPowL; uiX++)
        {
            *puiConnect++ = uiI0;
            *puiConnect++ = uiI1;
            *puiConnect++ = uiI2;
            *puiConnect++ = uiI1;
            *puiConnect++ = uiI3;
            *puiConnect++ = uiI2;
            uiI0++;
            uiI1++;
            uiI2++;
            uiI3++;
        }
    }
}
//----------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcBezierCylinder::Factory (MgcStream& rkStream)
{
    // MgcBezierCylinder is abstract, Factory never called
    return 0;
}
//---------------------------------------------------------------------------
void MgcBezierCylinder::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcBezierPatch::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_uiCylinderLevel);
}
//---------------------------------------------------------------------------
void MgcBezierCylinder::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcBezierPatch::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcBezierCylinder::Register (MgcStream& rkStream)
{
    return MgcBezierPatch::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcBezierCylinder::Save (MgcStream& rkStream)
{
    MgcBezierPatch::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_uiCylinderLevel);
}
//---------------------------------------------------------------------------
